package com.letv.android.client.episode.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.client.episode.PlayAlbumController;
import com.letv.android.client.episode.callback.ICommentItemClickObserver;
import com.letv.android.client.episode.callback.PlayAlbumControllerCallBack;
import com.letv.android.client.fragment.LetvBaseFragment;
import com.letv.android.client.view.PublicLoadLayout;
import com.letv.core.bean.AlbumNew;
import com.letv.core.bean.CommentBean;
import com.letv.core.db.PreferencesManager;
import com.letv.core.network.volley.Volley;
import com.letv.core.utils.LogInfo;

//import com.letv.android.client.episode.adapter.DetailHalfPlayCommentAdapter;
//import com.letv.android.client.episode.parser.CommentAddParser;
//import com.letv.android.client.episode.parser.CommentListParser;
//import com.letv.android.client.episode.parser.ReplyAddParser;
//import com.letv.android.client.episode.parser.ReplyListParser;
//import com.letv.android.client.episode.com.ledim.widget.ChannelListFootViewPlayerLibs;
//import com.letv.android.client.episode.com.ledim.widget.PullToRefreshBasePlayerLibs;
//import com.letv.android.client.episode.com.ledim.widget.PullToRefreshListViewPlayerLibs;
//import com.letv.android.client.fragment.introduction.ExpandListener;
//import com.letv.android.client.fragment.introduction.InitViewListener;
//import com.letv.android.client.fragment.introduction.IntroductionView;
//import com.letv.core.bean.CommentAddBean;
//import com.letv.core.bean.CommentListBean;
//import com.letv.core.bean.ReplyListBean;

/**
 * 详情.评论
 */
public class HalfPlayCommentFragment extends LetvBaseFragment implements OnClickListener, PlayAlbumControllerCallBack,
        ICommentItemClickObserver {

    /* ========= 布局变量 ========= */
    private PublicLoadLayout root;
    /**
     * 评论顶部算headView
     * */
    private View mHeadView;
    /**
     * 评论列表弹出框
     */
    private View popupView;
    /**
     * 简介view
     * */
//    private IntroductionView mIntroView;
    /**
     * 展开简介的线
     * */
    private ImageView mIntroExpandLineView;
    /**
     * 评论总数
     */
    private TextView totalCommentTextView;
    /**
     * 下拉刷新View
     */
//    private PullToRefreshListViewPlayerLibs pullToRefreshListView;
    /**
     * 评论列表ListView
     */
    private ListView listView;
    /**
     * 评论列表空提示
     */
    private LinearLayout nullTextTipLayout;
    /**
     * 评论复制TextView
     */
    private TextView copyTextView;
    /**
     * 评论赞一下TextView
     */
    private TextView likeTextView;
    /**
     * 评论回复TextView
     */
    private TextView replyTextView;
    /**
     * null评论icon
     */
    private ImageView commentTextView;
    /**
     * 底部加载更多View
     */
//    private ChannelListFootViewPlayerLibs listFootView;
    /**
     * 加载更多状态
     */
    private boolean isLoadingMore;

    /* ========= 适配器变量 ========= */
//    private DetailHalfPlayCommentAdapter mDetailHalfPlayCommentAdapterPlayerLibs;

    /* ========= 全局变量 ========= */
    public static final String BUNDLE_KEY_COMMENT_CONTENT = "comment_content";
    public static final String BUNDLE_KEY_IS_COMMENT = "is_comment";
    private PlayAlbumController mPlayAlbumController;
    /**
     * 新增评论异步任务
     */
    // private RequestAddCommentTaskPlayerLibs mAddCommentTask;
    /**
     * 新增回复异步任务
     */
    // private RequestAddReplyTaskPlayerLibs mAddReplyTask;
    /**
     * 获取评论列表异步任务
     */
    // private RequestCommentListTaskPlayerLibs mCommentListTask;
    /**
     * 获取回复列表异步任务
     */
    // private RequestReplytListTaskPlayerLibs mReplyListTask;
    /**
     * 点击评论弹出框
     */
    private PopupWindow mPopupWindow;
    /**
     * 系统剪贴板
     */
    @SuppressWarnings("deprecation")
    private ClipboardManager mClipBoard;

    private int page = 1;
    private int total = 0;
    private int commentNode;
    private int replyNode; // -1表示针对评论的回复，>0表示针对回复的回复

    private String content;
    private String commentId = "";
    private String replyId = "";

    private boolean isPullRefreshData = false;

    private String loadDataNonetText;

    /**
     * 点击回复更多的次数(统计专用)
     */
    private int moreCount = 0;

    /**
     * 是否可以开始加载数据
     */
    private boolean isInit;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
      /*  root = PublicLoadLayout.createPage(getActivity(), R.layout.detailplay_half_comment_playerlibs);
        mHeadView = UIsUtils.inflate(getActivity(), R.layout.detailplay_half_comment_head_playerlibs, null);
        popupView = UIsUtils.inflate(getActivity(), R.layout.detailplay_half_comment_item_toast_playerlibs, null);
        loadDataNonetText = LetvTools.getTextFromServer("100008",
                getActivity().getString(R.string.load_data_no_net_playerlibs));
        findview();*/
        return new TextView(getActivity());
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setOnClickListener();
        mPlayAlbumController = ((AlbumPlayActivity) getActivity()).getPlayAlbumController();
        mPlayAlbumController.commentCallBack = this;
        isInit = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getUserVisibleHint()) {
            showData();
        }
        // ((LetvBaseActivityObj) getActivity()).setCurrentPage(PageIdConstant.halpPlayPage);
        // LogInfo.log("glh", "HalfPlayCommentFragmentPlayerLibs...onresume="
        // + ((LetvBaseActivityObj) getActivity()).getCurrentPage());
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // 每次切换fragment时调用的方法
        if (isVisibleToUser) {
            showData();
        }

    }

    private void showData() {
        if (!isInit) {
            return;
        }

        isInit = false;
        page = 1;
      //  requestCommentList();
        requestIntroduceData();
    }

    public void requestIntroduceData() {
       /* mIntroView.init(String.valueOf(mPlayAlbumController.cid), String.valueOf(mPlayAlbumController.vid), "",
                String.valueOf(mPlayAlbumController.aid), null);*/
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Volley.getQueue().cancelWithTag("requestCommentList");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void findview() {
        // 下拉刷新view
       /* pullToRefreshListView = (PullToRefreshListViewPlayerLibs) root
                .findViewById(R.id.detailplay_half_comment_pulltorefreshlist);
        listView = pullToRefreshListView.getRefreshableView();
        nullTextTipLayout = (LinearLayout) mHeadView.findViewById(R.id.detailplay_half_comment_null_text);
        commentTextView = (ImageView) nullTextTipLayout.findViewById(R.id.textv_tip_comment);

        mIntroView = (IntroductionView) mHeadView.findViewById(R.id.detailplay_half_intro);
        mIntroExpandLineView = (ImageView) mHeadView.findViewById(R.id.detailplay_half_intro_line);
        // 评论总数view
        totalCommentTextView = (TextView) mHeadView.findViewById(R.id.textv_total_comment_num);
        listView.addHeaderView(mHeadView);
        pullToRefreshListView.setParams(new Object[] { true, "HalfPlayCommentFragmetnPlayerLibs" });

        listFootView = new ChannelListFootViewPlayerLibs(getActivity());
        // 评论列表Item
        copyTextView = (TextView) popupView.findViewById(R.id.textv_copy);
        likeTextView = (TextView) popupView.findViewById(R.id.textv_like);
        replyTextView = (TextView) popupView.findViewById(R.id.textv_reply);

        mIntroExpandLineView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                int wz;
                if (mIntroView.isExpand()) {
                    mIntroView.setExpand(false);
                    wz = 2;
                } else {
                    mIntroView.setExpand(true);
                    wz = 1;
                }
                if (mPlayAlbumController != null) {
                    StatisticsUtils.staticticsInfoPost(mPlayAlbumController.getActivity(), "0", "h30", null, wz, null,
                            PageIdConstant.halpPlayPage, mPlayAlbumController.cid + "", mPlayAlbumController.aid + "",
                            mPlayAlbumController.vid + "", null, "-");
                }
            }
        });
        mIntroView.setExpandListener(new ExpandListener() {
            @Override
            public void onExpand(boolean isExpand) {
                if (isExpand) {
                    mIntroExpandLineView.setBackgroundResource(R.drawable.intro_comment_hide);
                } else {
                    mIntroExpandLineView.setBackgroundResource(R.drawable.intro_comment_show);
                }
            }
        });
        mIntroView.setInitViewListener(new InitViewListener() {
            @Override
            public void onInitView(boolean isNeedExpand) {
                ViewGroup.LayoutParams params = mIntroExpandLineView.getLayoutParams();
                if (isNeedExpand) {
                    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    mIntroExpandLineView.setBackgroundResource(R.drawable.intro_comment_show);
                } else {
                    params.height = UIsUtils.dipToPx(2);
                    mIntroExpandLineView.setBackgroundResource(R.drawable.comment_lines);
                }
            }
        });*/
    }

    public static boolean needShowVideoIntro(long cid) {
        return (cid == AlbumNew.Channel.TYPE_JOY || cid == AlbumNew.Channel.TYPE_PE || cid == AlbumNew.Channel.TYPE_MUSIC);
    }

    public void setOnClickListener() {
      /*  pullToRefreshListView.setOnRefreshListener(onRefreshListener);
        pullToRefreshListView.setOnLastItemVisibleListener(onLastItemVisibleListener);
        root.setRefreshData(refreshData);
        commentTextView.setOnClickListener(this);*/
    }

    /**
     * 刷新listview列表.
     */
    public void refreshList() {
       /* if (isPullRefreshData) {
            isPullRefreshData = false;
            pullToRefreshListView.onRefreshComplete();
        }*/
    }

   /* public void requestCommentList() { // String pid, String vid, final int page, String cid
        isPullRefreshData = true;
        if (!(pullToRefreshListView.isRefreshing() || isLoadingMore)) {
            root.loading(true);
        }
        new LetvRequest<CommentListBean>(getActivity(), CommentListBean.class)
                .setRequestType(RequestManner.NETWORK_ONLY)
                // .setUrl("http://api.mob.app.letv.com/comment/list?page=1&devid=fc679b870ca10fb123999b785f2574d2&xid=22461379&pid=10006949&pcode=010110000&cid=1&rows=20&version=5.8")
                .setUrl(MediaAssetApi.getInstance().requestCommentListUrl(getActivity(),
                        String.valueOf(mPlayAlbumController.getPid()), String.valueOf(mPlayAlbumController.getVid()),
                        page, String.valueOf(mPlayAlbumController.getCid())))
                .setParser(new CommentListParser())
                .setTag("requestCommentList").setCallback(new SimpleResponse<CommentListBean>() {

                    @Override
                    public void onNetworkResponse(VolleyRequest<CommentListBean> request, CommentListBean result,
                            DataHull hull, NetworkResponseState state) {
                        isLoadingMore = false;
                        if (state == NetworkResponseState.SUCCESS) {
                            root.finish();
                            commentListResponse(result);
                        } else if (state == NetworkResponseState.NETWORK_NOT_AVAILABLE) {
                            isInit = true;
                            root.netError(false);
                            refreshList();
                        } else if (state == NetworkResponseState.NETWORK_ERROR) {
                            isInit = true;
                            root.netError(false);
                            refreshList();
                        } else if (state == NetworkResponseState.RESULT_ERROR) {
                            root.dataError(false);
                            refreshList();
                        }
                    }

                }).add();
    }

    private void commentListResponse(CommentListBean result) {
        root.loading(true, false);
        LogInfo.log("Emerson", "isPullRefreshData onPostExecute=====" + isPullRefreshData);
        if (isPullRefreshData) {
            isPullRefreshData = false;
            pullToRefreshListView.onRefreshComplete();
        }
        if (result != null && (result.total >0||(result.total ==0&&page==1&&result.data!=null&&result.data.size()>0))) {
            total = result.total;
            totalCommentTextView.setVisibility(View.VISIBLE);
            if (result.total ==0) {
                totalCommentTextView.setText("评论");
            }else{
                totalCommentTextView.setText(getActivity().getResources().getString(R.string.detail_comment_total_num_play,
                        EpisodeUtils.getPlayCountsToStr(result.total)));
            }
            nullTextTipLayout.setVisibility(View.GONE);
            if (mDetailHalfPlayCommentAdapterPlayerLibs == null) {
                mDetailHalfPlayCommentAdapterPlayerLibs = new DetailHalfPlayCommentAdapter(getActivity());
                if (result.data != null && result.data.size() > 0 && result.topCount != 0) {
                    mDetailHalfPlayCommentAdapterPlayerLibs.setTopCount(result.topCount);
                }
                mDetailHalfPlayCommentAdapterPlayerLibs.setObserver(HalfPlayCommentFragment.this);
            }

            if (page == 1) {
                mDetailHalfPlayCommentAdapterPlayerLibs.setVideoList(result.data);
                if (result.data != null && result.data.size() > 0
                        && (result.data.get(0).level == 1 || result.data.get(0).level == 2)) {
                    mDetailHalfPlayCommentAdapterPlayerLibs.setTopCount(result.topCount);
                } else {
                    mDetailHalfPlayCommentAdapterPlayerLibs.setTopCount(0);
                }
                if (listView.getFooterViewsCount() > 0) {
                    listView.removeFooterView(listFootView);
                }
                listView.addFooterView(listFootView);
                listView.setAdapter(mDetailHalfPlayCommentAdapterPlayerLibs);
            } else {
                mDetailHalfPlayCommentAdapterPlayerLibs.insertList(result.data);
                mDetailHalfPlayCommentAdapterPlayerLibs.notifyDataSetChanged();
            }
            if (result.data == null || (result.data != null && result.data.size() == 0)) {
                if (listView.getFooterViewsCount() > 0) {
                    LogInfo.log("Emerson", "-----------kbht 返回无效数据");
                    listView.removeFooterView(listFootView);
                }
            }
        } else {
            if (result != null&&result.total<=0&&page != 1) {
                totalCommentTextView.setVisibility(View.GONE);
                nullTextTipLayout.setVisibility(View.GONE);
            } else {
                total = result.total;
                totalCommentTextView.setVisibility(View.VISIBLE);
                totalCommentTextView.setText("评论");
                nullTextTipLayout.setVisibility(View.VISIBLE);
            }
            if (listView != null && listView.getFooterViewsCount() > 0) {
                listView.removeFooterView(listFootView);
            }
            if (page == 1) {
                listView.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return 0;
                    }

                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        return null;
                    }
                });
            }
        }
    }

    public void requestReplyList(String commentId) {
        new LetvRequest<ReplyListBean>(getActivity(), ReplyListBean.class).setRequestType(RequestManner.NETWORK_ONLY)
                .setUrl(MediaAssetApi.getInstance().requestReplyListUrl(commentId))
                .setCache(new VolleyDiskCache(getActivity(), "TabReplyList")).setParser(new ReplyListParser())
                .setCallback(new SimpleResponse<ReplyListBean>() {

                    @Override
                    public void onNetworkResponse(VolleyRequest<ReplyListBean> request, ReplyListBean result,
                            DataHull hull, NetworkResponseState state) {
                        if (state == NetworkResponseState.SUCCESS) {
                            replyListResponse(result);
                        }
                    }
                }).add();
    }

    private void replyListResponse(ReplyListBean result) {
        if (result != null && result.total > 0) {
            // 填充数据
            CommentBean commentBeanPlayerLibs = mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentNode);
            commentBeanPlayerLibs.replys.clear();
            for (ReplyBean reply : result.data) {
                commentBeanPlayerLibs.replys.addLast(reply);
            }
            mDetailHalfPlayCommentAdapterPlayerLibs.notifyDataSetChanged();
        }
    }
*/
   /* public void requestAddComment() {
        if (null == content || "".equals(content)) {
            return;
        }
        content = content.trim();
        if ("".equals(content.trim())) {
            return;
        }
        int cid = 0;
        if (mPlayAlbumController.getVideoBean() != null) {
            cid = mPlayAlbumController.getVideoBean().getCid();
        }
        addCommentPrexcute();
        
        new LetvRequest<CommentAddBean>(getActivity(), CommentAddBean.class)
                .setRequestType(RequestManner.NETWORK_ONLY)
                .addPostParams(MediaAssetApi.getInstance().addCommentUrlParam(String.valueOf(mPlayAlbumController.aid),
                        String.valueOf(mPlayAlbumController.vid), String.valueOf(cid), "cmt", content))
                .setUrl(MediaAssetApi.getInstance().addCommentUrl())
                .setCache(new VolleyDiskCache(getActivity(), "AddComment")).setParser(new CommentAddParser())
                .setCallback(new SimpleResponse<CommentAddBean>() {

                    @Override
                    public void onNetworkResponse(VolleyRequest<CommentAddBean> request, CommentAddBean result,
                            DataHull hull, NetworkResponseState state) {
                        if (state == NetworkResponseState.SUCCESS) {
                            addCommentResponse(result);
                        }
                    }

                    @Override
                    public void onErrorReport(VolleyRequest<CommentAddBean> request, String errorInfo) {
                        super.onErrorReport(request, errorInfo);
                    }

                }).add();

        // mAddCommentTask = new RequestAddCommentTaskPlayerLibs(getActivity(),
        // String.valueOf(mPlayAlbumController.aid),
        // String.valueOf(mPlayAlbumController.vid), String.valueOf(cid),
        // "cmt", content, mRequestAddCommentCallBack);
        // mAddCommentTask.start();
    }

    private void addCommentPrexcute() {
        if (mDetailHalfPlayCommentAdapterPlayerLibs == null) {
            total = 1;
            totalCommentTextView.setVisibility(View.VISIBLE);
            nullTextTipLayout.setVisibility(View.GONE);
            totalCommentTextView.setText(getActivity().getResources().getString(R.string.detail_comment_total_num_play,
                    total));
            mDetailHalfPlayCommentAdapterPlayerLibs = new DetailHalfPlayCommentAdapter(getActivity());
            mDetailHalfPlayCommentAdapterPlayerLibs.setObserver(HalfPlayCommentFragment.this);
            listView.setAdapter(mDetailHalfPlayCommentAdapterPlayerLibs);
        }
        if (mDetailHalfPlayCommentAdapterPlayerLibs.getTopCount() == 0) {
            LogInfo.log("Emerson", "---------getTopCount = 0 ");
            mDetailHalfPlayCommentAdapterPlayerLibs.getList().addFirst(createCommentBean(content));
        } else {
            LogInfo.log("Emerson", "---------getTopCount >>>>>");
            mDetailHalfPlayCommentAdapterPlayerLibs.getList().add(
                    mDetailHalfPlayCommentAdapterPlayerLibs.getTopCount(), createCommentBean(content));
        }
        mDetailHalfPlayCommentAdapterPlayerLibs.notifyDataSetChanged();
    }

    private void addCommentResponse(CommentAddBean result) {
        ToastUtils.showNormalToast(R.string.detail_comment_toast_commit_success_play);
    }
*/
   /* private void addCommentError() {
        ToastUtils.showToast(loadDataNonetText);
    }

    public void requestReplyComment() {
        int cid = 0;
        if (mPlayAlbumController.getVideoBean() != null) {
            cid = mPlayAlbumController.getVideoBean().getCid();
        }

        replyCommentPreexcute();
        new LetvRequest<CommentAddBean>(getActivity(), CommentAddBean.class)
                .setRequestType(RequestManner.NETWORK_ONLY)
                // .setUrl("http://api.mob.app.letv.com/play/tabs?pcode=010110000&version=5.9&devid=fc679b870ca10fb123999b785f2574d2&cid=0&zid=&vid=0&pid=10006909")
                .addPostParams(MediaAssetApi.getInstance().replyCommentUrlParams(String.valueOf(mPlayAlbumController.aid),
                        String.valueOf(mPlayAlbumController.vid), String.valueOf(cid), commentId, replyId, content))
                .setUrl(MediaAssetApi.getInstance().replyCommentUrl())
                .setCache(new VolleyDiskCache(getActivity(), "ReplyComment")).setParser(new ReplyAddParser())
                .setCallback(new SimpleResponse<CommentAddBean>() {

                    @Override
                    public void onNetworkResponse(VolleyRequest<CommentAddBean> request, CommentAddBean result,
                            DataHull hull, NetworkResponseState state) {
                        if (state == NetworkResponseState.SUCCESS) {
                            replyCommentResponse();
                        }
                    }

                    @Override
                    public void onCacheResponse(VolleyRequest<CommentAddBean> request, CommentAddBean result,
                            DataHull hull, CacheResponseState state) {
                        if (state == CacheResponseState.SUCCESS) {
                            replyCommentResponse();
                        }
                    }

                    @Override
                    public void onErrorReport(VolleyRequest<CommentAddBean> request, String errorInfo) {
                        super.onErrorReport(request, errorInfo);
                        
                    }

                }).add();
        // if (mAddReplyTask == null) {
        // mAddReplyTask = new RequestAddReplyTaskPlayerLibs(getActivity());
        // }
        // mAddReplyTask.setParams(String.valueOf(mPlayAlbumController.aid),
        // String.valueOf(mPlayAlbumController.vid), String.valueOf(cid),
        // commentId, replyId, content, mRequestAddReplyCallBack);
        // mAddReplyTask.start();
    }

    private void replyCommentPreexcute() {
        createReplyBean(commentNode, replyNode);
        mDetailHalfPlayCommentAdapterPlayerLibs.notifyDataSetChanged();
        ToastUtils.showNormalToast(R.string.detail_comment_toast_commit_success_play);
    }

    private void replyCommentResponse() {
*/



    public static onFragmentResult onFragmentResult;

    public interface onFragmentResult {

        void onFragmentResult_back(int requestCode, int resultCode, Intent data);
    }

    /**
     * 评论按钮点击事件触发
     * 
     * @param state
     */
    @Override
    public void notify(int state) {
        LogInfo.log("Emerson", "-----notify :state ");
        page =1;
        switch (state) {  // 通知评论页刷新评论
            case PlayAlbumControllerCallBack.STATE_FINISH:
                showData();
                break;
            case PlayAlbumControllerCallBack.STATE_REFRESH_DATA:
              /*  requestCommentList();
                mIntroView.init(String.valueOf(mPlayAlbumController.getCid()),
                        String.valueOf(mPlayAlbumController.getVid()), "",
                        String.valueOf(mPlayAlbumController.getPid()), null);
//                if (mPlayAlbumController != null && !mPlayAlbumController.isVideoNormal()) {
//                    setTabsDownloadState();
//                }*/
                break;
        }
        //dismissPopupWin();
    }

    @Override
    public void notify(Bundle bundle) {
        LogInfo.log("Emerson", "-----notify :bundle ");
        if (bundle != null) {
            content = bundle.getString(BUNDLE_KEY_COMMENT_CONTENT);
            boolean isComment = bundle.getBoolean(BUNDLE_KEY_IS_COMMENT);
            // 发送一个评论
            LogInfo.log("Emerson", "iscomment －－－－－－－－＞ " + isComment);
          /*  if (isComment) {
                requestAddComment();
            } else {
                requestReplyComment();
            }*/
            setCommentFragmentSelected();
        }
    }

    private TabsFragment getTabsFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        if (fm != null) {
            TabsFragment tabsFragment = (TabsFragment) fm.findFragmentByTag(TabsFragment.class.getSimpleName());
            if (tabsFragment != null) {
                return tabsFragment;
            }
        }
        return null;
    }

    private void setCommentFragmentSelected() {
        if (getTabsFragment() != null) {
//            getTabsFragment().setCommentFragmentSelected();
        }
    }

    private void replyText(String hint, boolean isComment) {
        if (getTabsFragment() != null) {
//            getTabsFragment().replyText(hint, isComment);
        }
    }
    
    private void setTabsDownloadState()  {
        if (getTabsFragment() != null) {
//            getTabsFragment().updateDownloadIconState();
        }
    }
    

    /**
     * 针对评论的Item点击事件
     */
    @Override
    public void onCommentItemClickEvent(View itemView, final TextView likeV, final int commentPos,
            final ImageView likeImage, final boolean isLike) {
        /*showPopupWindow(itemView);
        final CommentBean commentItem = (CommentBean) mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos);

        likeTextView.setText(getActivity().getResources().getString(
                isLike ? R.string.detail_comment_text_liked_play : R.string.detail_comment_text_like_play));
        if (isLike) {
            likeTextView.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    dismissPopupWin();
                }
                
            });
        } else {
            likeTextView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // 赞or取消赞
                    dismissPopupWin();
                    boolean ok = mDetailHalfPlayCommentAdapterPlayerLibs.actionLike(likeV, true, commentPos, 0,
                            commentItem.like, likeImage);
                    // if (ok) {
                    // mDetailHalfPlayCommentAdapterPlayerLibs
                    // .showLetvToast(getActivity()
                    // .getResources()
                    // .getString(
                    // R.string.detail_comment_toast_like));
                    // }
                    // 5.7需求
                    LogInfo.LogStatistics("赞一下");
                    StatisticsUtils.staticticsInfoPost(getActivity(), "24", "82", null, commentPos + 1, null,
                            PageIdConstant.halpPlayPage, null, null, null, null, null);
                }
            });
        }

        replyTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismissPopupWin();
                replyText(
                        getActivity().getString(R.string.detail_half_comment_edit_text_hint_reply,
                                handleUserName(commentItem.user.username)), false);
                commentNode = commentPos;
                replyNode = -1;
                commentId = commentItem._id;
                // 5.7需求
                LogInfo.LogStatistics("回复");
                StatisticsUtils.staticticsInfoPost(getActivity(), "0", "82", null, commentNode + 1, null,
                        PageIdConstant.halpPlayPage, null, null, null, null, null);
            }
        });

        copyTextView.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // 复制评论到剪贴板
                dismissPopupWin();
                *//**
                 * android.content.ClipboardManager is only available on API
                 * level 11 so, there, we use android.text.ClipboardManager
                 *//*
                if (mClipBoard == null) {
                    mClipBoard = (ClipboardManager) getActivity().getSystemService(getActivity().CLIPBOARD_SERVICE);
                }
                mClipBoard.setText(commentItem.content);

                mDetailHalfPlayCommentAdapterPlayerLibs.showLetvToast(getActivity().getResources().getString(
                        R.string.detail_comment_toast_copy_play));
            }
        });*/
    }

    /**
     * 针对回复的Item点击事件
     */
    @Override
    public void onReplyItemClickEvent(View itemView, final TextView likeV, final int commentPos, final int replyPos,
            final ImageView likeImage, final boolean isLike) {
      /*  LogInfo.log("LM", "onReplyItemClickEvent replyPos " + replyPos + "  commentPos  " + commentPos);
        showPopupWindow(itemView);
        final ReplyBean replyItem = ((CommentBean) mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos)).replys
                .get(replyPos);

        likeTextView.setText(getActivity().getResources().getString(
                isLike ? R.string.detail_comment_text_liked_play : R.string.detail_comment_text_like_play));
        if (isLike) {
            likeTextView.setOnClickListener(null);
        } else {
            likeTextView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // 赞or取消赞
                    dismissPopupWin();
                    boolean ok = mDetailHalfPlayCommentAdapterPlayerLibs.actionLike(likeV, false, commentPos, replyPos,
                            replyItem.like, likeImage);
                    // if (ok) {
                    // mDetailHalfPlayCommentAdapterPlayerLibs
                    // .showLetvToast(getActivity()
                    // .getResources()
                    // .getString(
                    // R.string.detail_comment_toast_like));
                    // }
                }
            });
        }

        replyTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismissPopupWin();
                replyText(
                        getActivity().getString(R.string.detail_half_comment_edit_text_hint_reply,
                                handleUserName(replyItem.user.username)), false);
                commentNode = commentPos;
                replyNode = replyPos;
                commentId = replyItem.commentid;
                replyId = replyItem._id;
            }
        });

        copyTextView.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // 复制评论到剪贴板
                dismissPopupWin();
                *//**
                 * android.content.ClipboardManager is only available on API
                 * level 11 so, there, we use android.text.ClipboardManager
                 *//*
                if (mClipBoard == null) {
                    mClipBoard = (ClipboardManager) getActivity().getSystemService(getActivity().CLIPBOARD_SERVICE);
                }
                mClipBoard.setText(replyItem.content);

                mDetailHalfPlayCommentAdapterPlayerLibs.showLetvToast(getActivity().getResources().getString(
                        R.string.detail_comment_toast_copy_play));
            }
        });*/
    }

    @Override
    public void onExpendMoreReplyClickEvent(int commentPos, String commentId, boolean isExpend) {
        // // 展开更多回复
        // commentNode = commentPos;
        // requestReplyList(commentId);
      /*  if (isExpend) {
            // 展开更多回复
            commentNode = commentPos;
            requestReplyList(commentId);
            moreCount++;
            // add 5.9 查看更多回复
            StatisticsUtils.staticticsInfoPost(getActivity(), "0", "87", null, moreCount, null,
                    PageIdConstant.halpPlayPage, "-", "-", "-", "-", "-");
        } else {
            // 收起更多回复
            CommentBean commentBeanPlayerLibs = mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos);
            int num = commentBeanPlayerLibs.replys.size();
            if (num < mDetailHalfPlayCommentAdapterPlayerLibs.MAX_RPLEY_SHOW) {
                return;
            }

            for (int i = num; i > 0; --i) {
                if (i == mDetailHalfPlayCommentAdapterPlayerLibs.MAX_RPLEY_SHOW) {
                    break;
                }
                commentBeanPlayerLibs.replys.removeLast();
            }
            mDetailHalfPlayCommentAdapterPlayerLibs.notifyDataSetChanged();
        }*/
    }

    public void showPopupWindow(View target) {
      /*  if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(getActivity());
            mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            mPopupWindow.setWidth(UIsUtils.zoomWidth(285));
            mPopupWindow.setHeight(UIsUtils.zoomWidth(40));
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setContentView(popupView);
        }
        final int[] location = new int[2];
        target.getLocationOnScreen(location);
        LogInfo.log("Emerson", "-----------location[1] = " + location[1]);
        int popLocationY = location[1] - UIsUtils.dipToPx(22);
        mPopupWindow.showAtLocation(target,Gravity.TOP, 0, popLocationY);// 需要指定Gravity，默认情况是center.
        mPopupWindow.update();
    }

    public void dismissPopupWin() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }*/
    }

    private CommentBean createCommentBean(String content) {
      /*  CommentBean newComment = new CommentBean();
        newComment.content = content;
        newComment.user = new CommentBean.User();
        newComment.user.uid = PreferencesManager.getInstance().getUserId();
        newComment.user.photo = PreferencesManager.getInstance().getUserIcon();
        newComment.user.username = PreferencesManager.getInstance().getNickName();
        newComment.vtime = getActivity().getResources().getString(R.string.record_date_now);

        return newComment;*/
        return null;
    }

    // 构造本地数据

    @Override
    public void onPause() {
        super.onPause();
    }

    public interface SetSendRlState {

        void setSoftVisibile();
    }

    @Override
    public void onClick(View arg0) {

    }

    private String handleUserName(String userName) {
        if (userName != null && (!TextUtils.isEmpty(userName.trim()))) {
            if (userName.trim().length() > 18) {
                userName = userName.trim().substring(0, 18) + "...";
            }
        }
        return userName;
    }

    @Override
    public void onCommentEditClickEvent(int commentPos, int replyPos, boolean isComment) {
       /* if (isComment) {
            CommentBean commentItem = (CommentBean) mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos);
            replyText(
                    mPlayAlbumController.getActivity().getString(R.string.detail_half_comment_edit_text_hint_reply,
                            handleUserName(commentItem.user.username)), false);
            commentNode = commentPos;
            replyNode = -1;
            commentId = commentItem._id;
        } else {
            ReplyBean replyItem = null;
            if (mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos).replys != null) {
                replyItem = (ReplyBean) mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos).replys
                        .get(replyPos);
            }
            replyText(
                    mPlayAlbumController
                            .getActivity()
                            .getString(
                                    R.string.detail_half_comment_edit_text_hint_reply,
                                    handleUserName(((mDetailHalfPlayCommentAdapterPlayerLibs.getItem(commentPos).replys) != null ? replyItem.user.username
                                            : ""))), isComment);
            commentNode = commentPos;
            replyNode = replyPos;
            commentId = replyItem.commentid;
            replyId = replyItem._id;
        }*/
    }

 //   private HalfPlayCommentLikeFragment mLikeFragment;

    private void logoutLikeDialog() {
        if (PreferencesManager.getInstance().isLogoutCommentLikeDialogVisible()) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            if (fm != null) {
              /*  FragmentTransaction ft = fm.beginTransaction();
                mLikeFragment = (HalfPlayCommentLikeFragment) fm.findFragmentByTag("showLikeLogout");
                if (null == mLikeFragment) {
                    mLikeFragment = new HalfPlayCommentLikeFragment();
                } else {
                    ft.remove(mLikeFragment);
                }
                ft.add(mLikeFragment, "showLikeLogout");
                ft.commitAllowingStateLoss();*/
            }
        }
        PreferencesManager.getInstance().setLogoutCommentLikeCount();
    }

    @Override
    public void onLogoutLikeClickEvent() {
        logoutLikeDialog();
    }

    @Override
    public String getTagName() {
        return HalfPlayCommentFragment.class.getSimpleName();
    }

    @Override
    public int getContainerId() {
        return 0;
    }

    // private void onLoginSuccess(TextView halfPlayControllerEdit){
    // if(PlayAlbumController.sIsLoginForEditComment){
    // if(PreferencesManager.getInstance().isLogin()){
    // halfPlayControllerEdit.setText(R.string.detail_half_comment_edit_text_hint);
    // }else{
    // halfPlayControllerEdit.setText(R.string.detail_half_comment_edit_text_hint_logout);
    // }
    // if (!PlayAlbumController.mIsComment&&!TextUtils.isEmpty(PlayAlbumController.mHint)) {
    // replyText(PlayAlbumController.mHint, PlayAlbumController.mIsComment);
    // PlayAlbumController.mIsComment = true;
    // PlayAlbumController.mHint = "";
    // }else{
    // replyText(getString(R.string.detail_half_comment_edit_text_hint), PlayAlbumController.mIsComment);
    // }
    // PlayAlbumController.sIsLoginForEditComment = false;
    // }
    // }
    //
    //
    // public void replyText(String hint, boolean isComment) {
    //
    // if (PreferencesManager.getInstance().isLogin()) {
    // if (!NetworkUtils.isNetworkAvailable()) {
    // ToastUtils.showToast(mOptionBottomNoNetText);
    // return;
    // }
    // FragmentManager fm = this.getChildFragmentManager();
    // if (fm != null) {
    // FragmentTransaction ft = fm.beginTransaction();
    // mPlayAlbumController.sCommentSoftKeyboard = (HalfPlaySoftKeyboardFragment)
    // fm.findFragmentByTag("showSoftKeybord");
    // if (null == mPlayAlbumController.sCommentSoftKeyboard) {
    // mPlayAlbumController.sCommentSoftKeyboard = new HalfPlaySoftKeyboardFragment();
    // } else {
    // ft.remove(mPlayAlbumController.sCommentSoftKeyboard);
    // }
    // Bundle bundle = new Bundle();
    // bundle.putString(HalfPlaySoftKeyboardFragment.BUNDLE_KEY_HINT, hint);
    // bundle.putBoolean(HalfPlayCommentFragment.BUNDLE_KEY_IS_COMMENT, isComment);
    // mPlayAlbumController.sCommentSoftKeyboard.setMyBundleParam(bundle);
    // mPlayAlbumController.sCommentSoftKeyboard.setController(mPlayAlbumController);
    // mPlayAlbumController.sCommentSoftKeyboard.setSoftState(new SetSendRlState() {
    // @Override
    // public void setSoftVisibile() {
    //
    // }
    // });
    // ft.add(mPlayAlbumController.sCommentSoftKeyboard, "showSoftKeybord");
    // ft.commitAllowingStateLoss();
    // }
    // } else {
    // PlayAlbumController.mHint = hint;
    // PlayAlbumController.mIsComment = isComment;
    // PlayAlbumController.sIsLoginForEditComment = true;
    //
    // /*
    // * 跳转到登录页面
    // */
    // // startActivityForResult(new Intent(getActivity(), LetvAccountLogin.class), RESULT_OK);
    // startActivity(new Intent(getActivity(), LetvAccountLogin.class));
    //
    // // LetvSdkPlayerLibs.getInstance().LetvAccountLoginLaunch(
    // // mPlayAlbumController.getActivity(), 1);
    // }
    // }

}
